package com.loe.mvp.util.sp

import org.json.JSONArray
import org.json.JSONObject
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/** 公共Sp */
object SpPublic: SpDelegate()

/** 公共Json Sp */
object SpJsonPublic: SpDelegate()

/** 公共Sp钥匙，用于隔离sp空间 */
var spKey = ""

/**
 * SP委托扩展
 *
 * @author zls
 * @since 2020/9/22-11:01
 */
fun spString(def: String = "") = object : ReadWriteProperty<Any?, String>
{
    override fun getValue(thisRef: Any?, property: KProperty<*>): String
    {
        return SpPublic.getString(thisRef?.javaClass?.name + "-$spKey-" + property.name, def)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: String)
    {
        SpPublic.putString(thisRef?.javaClass?.name + "-$spKey-" + property.name, value)
    }
}

fun spInt(def: Int = 0) = object : ReadWriteProperty<Any?, Int>
{
    override fun getValue(thisRef: Any?, property: KProperty<*>): Int
    {
        return SpPublic.getInt(thisRef?.javaClass?.name + "-$spKey-" + property.name, def)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int)
    {
        SpPublic.putInt(thisRef?.javaClass?.name + "-$spKey-" + property.name, value)
    }
}

fun spLong(def: Long = 0L) = object : ReadWriteProperty<Any?, Long>
{

    override fun getValue(thisRef: Any?, property: KProperty<*>): Long
    {
        return SpPublic.getLong(thisRef?.javaClass?.name + "-$spKey-" + property.name, def)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: Long)
    {
        SpPublic.putLong(thisRef?.javaClass?.name + "-$spKey-" + property.name, value)
    }
}

fun spFloat(def: Float = 0.0f) = object : ReadWriteProperty<Any?, Float>
{
    override fun getValue(thisRef: Any?, property: KProperty<*>): Float
    {
        return SpPublic.getFloat(thisRef?.javaClass?.name + "-$spKey-" + property.name, def)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: Float)
    {
        SpPublic.putFloat(thisRef?.javaClass?.name + "-$spKey-" + property.name, value)
    }
}

fun spDouble(def: Double = 0.0) = object : ReadWriteProperty<Any?, Double>
{
    override fun getValue(thisRef: Any?, property: KProperty<*>): Double
    {
        return SpPublic.getDouble(thisRef?.javaClass?.name + "-$spKey-" + property.name, def)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: Double)
    {
        SpPublic.putDouble(thisRef?.javaClass?.name + "-$spKey-" + property.name, value)
    }
}

fun spBoolean(def: Boolean = false) = object : ReadWriteProperty<Any?, Boolean>
{
    override fun getValue(thisRef: Any?, property: KProperty<*>): Boolean
    {
        return SpPublic.getBoolean(thisRef?.javaClass?.name + "-$spKey-" + property.name, def)
    }

    override fun setValue(
        thisRef: Any?,
        property: KProperty<*>,
        value: Boolean
    )
    {
        SpPublic.putBoolean(thisRef?.javaClass?.name + "-$spKey-" + property.name, value)
    }
}

fun spJsonArray() = object : ReadWriteProperty<Any?, JSONArray>
{
    override fun getValue(thisRef: Any?, property: KProperty<*>): JSONArray
    {
        return SpJsonPublic.getJsonArray(thisRef?.javaClass?.name + "-$spKey-" + property.name)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: JSONArray)
    {
        SpJsonPublic.putJsonArray(thisRef?.javaClass?.name + "-$spKey-" + property.name, value)
    }
}

fun spJson() = object : ReadWriteProperty<Any?, JSONObject>
{
    override fun getValue(thisRef: Any?, property: KProperty<*>): JSONObject
    {
        return SpJsonPublic.getJson(thisRef?.javaClass?.name + "-$spKey-" + property.name)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: JSONObject)
    {
        SpJsonPublic.putJson(thisRef?.javaClass?.name + "-$spKey-" + property.name, value)
    }
}